﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BusinessMapping.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines type
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business.Mapping
{
    using System;
    using System.Diagnostics;
    using AutoMapper;
    using Entities;

    /// <summary>
    /// Defines type <see cref="BusinessMapping"/>
    /// </summary>
    [DebuggerStepThrough]
    public class BusinessMapping : Profile
    {
        /// <summary>
        /// Configures this instance.
        /// </summary>
        protected override void Configure()
        {
            CreateMap<Data.Entities.Account, Account>()
                .ForMember(s => s.AccountId, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.AccountType, o => o.MapFrom(d => d.AccountType))
                .ForMember(s => s.UserId, o => o.MapFrom(d => d.OwnerId))
                .ForMember(s => s.AccountNumber, o => o.MapFrom(d => d.AccountNumber))
                .ForMember(s => s.InitialBalanceDate, o => o.Ignore())
                .ForMember(s => s.InitialBalance, o => o.Ignore())
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name));
            CreateMap<Account, Data.Entities.Account>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.AccountId))
                .ForMember(s => s.AccountType, o => o.MapFrom(d => d.AccountType))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.AccountNumber, o => o.MapFrom(d => d.AccountNumber))
                .ForMember(s => s.Transactions, o => o.Ignore())
                .ForMember(s => s.OwnerId, o => o.MapFrom(d => d.UserId))
                .ForMember(s => s.IsClosed, o => o.Ignore())
                .ForMember(s => s.Targets, o => o.Ignore())
                .ForMember(s => s.User, o => o.Ignore())
                .ForMember(s => s.AccountType1, o => o.Ignore());

            CreateMap<Data.Entities.AccountType, AccountType>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name));
            CreateMap<AccountType, Data.Entities.AccountType>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.Accounts, o => o.Ignore());

            CreateMap<Data.Entities.Tag, Tag>()
                .ForMember(s => s.TagId, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.UserId, o => o.MapFrom(d => d.Owner));
            CreateMap<Tag, Data.Entities.Tag>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.TagId))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.Owner, o => o.MapFrom(d => d.UserId))
                .ForMember(s => s.Targets, o => o.Ignore())
                .ForMember(s => s.User, o => o.Ignore())
                .ForMember(s => s.Transactions, o => o.Ignore());

            CreateMap<Data.Entities.Transaction, Transaction>()
                .ForMember(s => s.TransactionId, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Title, o => o.MapFrom(d => d.Title))
                .ForMember(s => s.Details, o => o.MapFrom(d => d.Details))
                .ForMember(s => s.Amount, o => o.MapFrom(d => Math.Abs(d.Amount)))
                .ForMember(s => s.Account, o => o.MapFrom(d => d.Account))
                .ForMember(s => s.Date, o => o.MapFrom(d => d.Date))
                .ForMember(s => s.Tags, o => o.MapFrom(d => d.Tags))
                .ForMember(s => s.TransactionType, o => o.MapFrom(d => d.Amount < 0 ? TransactionTypes.Expanse : TransactionTypes.Income))
                .ForMember(s => s.LinkedTransactionAccount, o => o.Ignore())
                .ForMember(s => s.LinkedTransactionDate, o => o.Ignore())
                .ForMember(s => s.IsTransferTransaction, o => o.MapFrom(d => d.RelatedTransactionId.HasValue && d.RelatedTransactionId.Value > 0 ? true : false))
                .ForMember(s => s.RelatedTransactionId, o => o.MapFrom(d => d.RelatedTransactionId));

            CreateMap<Transaction, Data.Entities.Transaction>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.TransactionId))
                .ForMember(s => s.Title, o => o.MapFrom(d => d.Title))
                .ForMember(s => s.Details, o => o.MapFrom(d => d.Details))
                .ForMember(s => s.Amount, o => o.MapFrom(d => d.TransactionType == TransactionTypes.Income ? d.Amount : -d.Amount))
                .ForMember(s => s.Account, o => o.MapFrom(d => d.Account))
                .ForMember(s => s.Date, o => o.MapFrom(d => d.Date))
                .ForMember(s => s.Tags, o => o.MapFrom(d => d.Tags))
                .ForMember(s => s.RelatedTransactionId, o => o.MapFrom(d => d.RelatedTransactionId))
                .ForMember(s => s.VehicleTransactions, o => o.Ignore())
                .ForMember(s => s.Account1, o => o.Ignore());

            CreateMap<Data.Entities.Vehicle, Vehicle>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Title))
                .ForMember(s => s.UserId, o => o.MapFrom(d => d.UserId));
            CreateMap<Vehicle, Data.Entities.Vehicle>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Title, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.UserId, o => o.MapFrom(d => d.UserId))
                .ForMember(s => s.User, o => o.Ignore())
                .ForMember(s => s.VehicleTransactions, o => o.Ignore());

            CreateMap<Data.Entities.VehicleTransaction, VehicleTransaction>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.TransactionId, o => o.MapFrom(d => d.TransactionId))
                .ForMember(s => s.Milage, o => o.MapFrom(d => d.Milage))
                .ForMember(s => s.Fill, o => o.MapFrom(d => d.Fill))
                .ForMember(s => s.Oil, o => o.MapFrom(d => d.Oil))
                .ForMember(s => s.Notes, o => o.Ignore())
                .ForMember(s => s.Account, o => o.Ignore())
                .ForMember(s => s.Date, o => o.Ignore())
                .ForMember(s => s.Service, o => o.MapFrom(d => d.Service));
            CreateMap<VehicleTransaction, Data.Entities.VehicleTransaction>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.TransactionId, o => o.MapFrom(d => d.TransactionId))
                .ForMember(s => s.Milage, o => o.MapFrom(d => d.Milage))
                .ForMember(s => s.Fill, o => o.MapFrom(d => d.Fill))
                .ForMember(s => s.Oil, o => o.MapFrom(d => d.Oil))
                .ForMember(s => s.Service, o => o.MapFrom(d => d.Service))
                .ForMember(s => s.VehicleId, o => o.MapFrom(d => d.VehicleId))
                .ForMember(s => s.Transaction, o => o.Ignore())
                .ForMember(s => s.Vehicle, o => o.Ignore());

            CreateMap<Data.Entities.AccountMonthlyTotal, AccountMonthlyTotal>()
                .ForMember(s => s.Month, o => o.MapFrom(d => new DateTime(d.Year, d.Month, 1)))
                .ForMember(s => s.MonthlyTotal, o => o.MapFrom(d => d.MonthlyTotal))
                .ForMember(s => s.CumulativeTotal, o => o.MapFrom(d => d.CumulativeTotal));
            CreateMap<AccountMonthlyTotal, Data.Entities.AccountMonthlyTotal>()
                .ForMember(s => s.Month, o => o.MapFrom(d => d.Month.Month))
                .ForMember(s => s.Year, o => o.MapFrom(d => d.Month.Year))
                .ForMember(s => s.MonthlyTotal, o => o.MapFrom(d => d.MonthlyTotal))
                .ForMember(s => s.CumulativeTotal, o => o.MapFrom(d => d.CumulativeTotal));

            CreateMap<Data.Entities.User, User>()
                .ForMember(s => s.UserId, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name));
            CreateMap<User, Data.Entities.User>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.UserId))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.Accounts, o => o.Ignore())
                .ForMember(s => s.Password, o => o.Ignore())
                .ForMember(s => s.Vehicles, o => o.Ignore())
                .ForMember(s => s.SecretQuestion, o => o.Ignore())
                .ForMember(s => s.SecretAnswer, o => o.Ignore())
                .ForMember(s => s.CurrencyLocale, o => o.Ignore())
                .ForMember(s => s.Tags, o => o.Ignore());

            CreateMap<Data.Entities.Target, Target>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Amount, o => o.MapFrom(d => d.Amount))
                .ForMember(s => s.TagId, o => o.MapFrom(d => d.TagId))
                .ForMember(s => s.TargetDate, o => o.MapFrom(d => d.TargetDate))
                .ForMember(s => s.TargetType, o => o.MapFrom(d => d.TargetType))
                .ForMember(s => s.Title, o => o.MapFrom(d => d.Title))
                .ForMember(s => s.AccountId, o => o.MapFrom(d => d.AccountId));
            CreateMap<Target, Data.Entities.Target>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Amount, o => o.MapFrom(d => d.Amount))
                .ForMember(s => s.TagId, o => o.MapFrom(d => d.TagId))
                .ForMember(s => s.TargetDate, o => o.MapFrom(d => d.TargetDate))
                .ForMember(s => s.TargetType, o => o.MapFrom(d => d.TargetType))
                .ForMember(s => s.Title, o => o.MapFrom(d => d.Title))
                .ForMember(s => s.AccountId, o => o.MapFrom(d => d.AccountId))
                .ForMember(s => s.TargetType1, o => o.Ignore())
                .ForMember(s => s.Account, o => o.Ignore())
                .ForMember(s => s.Tag, o => o.Ignore())
                .ForMember(s => s.TargetType1, o => o.Ignore());

            this.CreateMap<Data.Entities.Bill, Bill>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.Amount, o => o.MapFrom(d => d.Amount))
                .ForMember(s => s.DueDate, o => o.MapFrom(d => d.DueDate))
                .ForMember(s => s.ExpiryDate, o => o.MapFrom(d => d.ExpiryDate))
                .ForMember(s => s.Paid, o => o.MapFrom(d => d.Paid))
                .ForMember(s => s.Recurring, o => o.MapFrom(d => d.Recurring))
                .ForMember(s => s.RecurringFequency, o => o.MapFrom(d => d.RecurringFequency));
            CreateMap<Bill, Data.Entities.Bill>()
                .ForMember(s => s.Id, o => o.MapFrom(d => d.Id))
                .ForMember(s => s.Name, o => o.MapFrom(d => d.Name))
                .ForMember(s => s.Amount, o => o.MapFrom(d => d.Amount))
                .ForMember(s => s.DueDate, o => o.MapFrom(d => d.DueDate))
                .ForMember(s => s.ExpiryDate, o => o.MapFrom(d => d.ExpiryDate))
                .ForMember(s => s.Paid, o => o.MapFrom(d => d.Paid))
                .ForMember(s => s.Recurring, o => o.MapFrom(d => d.Recurring))
                .ForMember(s => s.RecurringFequency, o => o.MapFrom(d => d.RecurringFequency));

            Mapper.AssertConfigurationIsValid();
        }
    }
}